Page({
    /**
     * 页面的初始数据
     */
    data: {
        isOpen: true,
        url: "",
        tempPhoto:"",
        statusBarHeight: "",
        list: [],
        origin_color: "#2f8ff6",

    },
    /**
     * 生命周期函数--监听页面显示
     */

    onShow() {
        let list = require("./list").default.list
        // console.log("list", list)
        let tempPhoto = getApp().globalData.tempPhoto
       


        wx.getSystemInfo({
            success: res => {
                // console.log(res)
                this.setData({
                    photoHeight: res.screenHeight - 180 + "px",
                    statusBarHeight: res.statusBarHeight, 
                    list: list,
                    url:tempPhoto,
                    tempPhoto:tempPhoto
                })
            },
        })

    },
    resetO() {
        let list = JSON.parse(JSON.stringify(this.data.list))
        list.forEach(item => {
            item.select = false
        })
        this.setData({
            list: list,
            origin_color: "#2f8ff6",
            url:this.data.tempPhoto
        })
    },

    previewImg(){
       wx.previewImage({
         urls: [this.data.url],
       })


    },
   
    exchangeCS(e) {
        wx.showLoading({
            title: '解析中...',
        })
        let list = JSON.parse(JSON.stringify(this.data.list))
        list.forEach(item => {
            item.select = false
        })
        list.forEach(item => {
            if (item.id == e.currentTarget.dataset.item.id) {
                item.select = true
            }
        })
        this.setData({
            list: list,
            origin_color: "grey"
        })
        wx.getFileSystemManager().readFile({
            filePath: this.data.tempPhoto, //选择图片返回的相对路径
            encoding: 'base64', //编码格式
            success: res => { //成功的回调
                let data = {
                    openid: wx.getStorageSync('ACCESS_TOKEN'),
                    nickname: wx.getStorageSync('appUserInfo').nickName,
                    avatar: wx.getStorageSync('appUserInfo').avatarUrl,
                    unionid: wx.getStorageSync('appUserInfo').union_id,
                    base64String: res.data,
                    R:e.currentTarget.dataset.item.r,
                    G:e.currentTarget.dataset.item.g,
                    B:e.currentTarget.dataset.item.b
                }

                wx.request({
                    method: "POST",
                    url: getApp().zxsz,
                    data: data,
                    header: {
                        'content-type': 'application/json'
                    },
                    success: (result => {
                        // console.log("res", result)
                        wx.hideLoading()
                        if (result.data.code == '0') {
                            this.setData({
                                url:result.data.lip_img_url
                            })

                        } else {
                            // let msg = result.data.msg
                            wx.showToast({
                                title: "图片中没有人脸！",
                                icon: 'none',
                                duration: 2000
                            })
                        }
                    }),
                    fail: (fail => {
                        wx.hideLoading()
                        // console.log("err", fail)
                    })
                })
            }
        })
    },
    // 获取唇色列表
    getList() {
        wx.request({
            url: getApp().zxsz_cs_list,
            method: "GET",
            data: {},
            header: {
                'content-type': 'application/json'
            },
            success: (result => {
                // console.log("res", result)
            }),
            fail: (fail => {

                // console.log("err", fail)
            })
        })
    },
    naviback() {
        wx.navigateBack({
            delta: 1,
        })
    }
})